package com.sky.controller.user;

import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.UUID;

@Api(tags = "C端订单接口")
@Slf4j
@RestController(value = "userOrderController")
@RequestMapping("/user/order")
public class OrderController {

    @Autowired
    private OrderService orderService;
    @ApiOperation("提交用户订单")
    @PostMapping("/submit")
    public Result<OrderSubmitVO> submitOrder(@RequestBody OrdersSubmitDTO ordersSubmitDTO){
        //实现该方法并返回结果
        OrderSubmitVO orderSubmitVO = orderService.submitOrder(ordersSubmitDTO);
        return Result.success(orderSubmitVO);
    }

    @ApiOperation("订单支付")
    @PutMapping("/payment")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO){
        log.info("订单支付：{}", ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO =  new OrderPaymentVO();
        //伪造的预支付参数（跳过api调用）
        orderPaymentVO.setTimeStamp(String.valueOf(System.currentTimeMillis()));//使用系统的事件戳
        orderPaymentVO.setNonceStr(UUID.randomUUID().toString().substring(0,32));
        orderPaymentVO.setPackageStr("prepay_id=MOCK_" + System.currentTimeMillis());
        orderPaymentVO.setSignType("RSA");
        orderPaymentVO.setPaySign("MOCK_SIGN_" + UUID.randomUUID());
        return Result.success(orderPaymentVO);
    }
    @PostMapping("/pay/mock")
    @ApiOperation("微信支付模拟接口")
    public Result<String> mockPaySuccess(@RequestParam String number){
        orderService.paySuccess(number);
        return Result.success("模拟支付成功");
    }

    @GetMapping("/historyOrders")
    @ApiOperation("查询历史订单")
    public Result<PageResult> queryHistoryOrders(@RequestParam Integer page, @RequestParam Integer pageSize, @RequestParam(required = false) Integer status){
       PageResult pageResult = orderService.queryHistoryOrders(page,pageSize,status);
       return Result.success(pageResult);
    }

    @ApiOperation("查询订单详情")
    @GetMapping("/orderDetail/{id}")
    public Result<OrderVO> queryOrderDetail(@PathVariable Long id){
        //根据订单的id进行查询订单的数据,只能查找到一个对象而不是列表
        OrderVO orderVO =   orderService.queryOrderDetail(id);
        return Result.success(orderVO);
    }

    @ApiOperation("取消订单")
    @PutMapping("/cancel/{id}")
    public Result<String> cancelOrder(@PathVariable Long id){
        orderService.cancelOrder(id);
        return Result.success();
    }

    @ApiOperation("再来一单")
    @PostMapping("/repetition/{id}")
    public Result<String> repetitionOrder(@PathVariable Long id){
        orderService.repetitionOrder(id);
        return Result.success();
    }

    @ApiOperation("客户催单")
    @GetMapping("/reminder/{id}")
    public Result<String> reminderOrder(@PathVariable Long id){
        orderService.reminderOrder(id);
        return Result.success();
    }

}
